﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.Video;

namespace GUI.Controls.Video
{
    public partial class BoxVideo3 : System.Web.UI.UserControl
    {
        private const int ImageWidth1 = 330;
        private const int ImageHeight1 = 230;
        private const int ImageWidth2 = 120;
        private const int ImageHeight2 = 75;

        private const string TopItemFormat = "{3}<h2><a href=\"{0}\" title=\"{2}\">{1}</a></h2>";
        private const string LiFormat = "<li>{3}<a href=\"{0}\" title=\"{2}\">{1}</a></li>";

        private const int Top = 4;

        protected void Page_Load(object sender, EventArgs e)
        {
            var dt = VideoBO.GetEmbedVideo(Top, DefinedConstants.Video.Types.HomeEmbed);
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    int imageWidth;
                    int imageHeight;
                    if (i == 0)
                    {
                        imageWidth = ImageWidth1;
                        imageHeight = ImageHeight1;
                    }
                    else
                    {
                        imageWidth = ImageWidth2;
                        imageHeight = ImageHeight2;
                    }

                    var avatar = PresentationUtils.Photo.BuildPhoto(
                        r[SqlConstants.Video.Avatar].ToString(),
                        r[SqlConstants.Video.Name].ToString(),
                        r[SqlConstants.Video.Url].ToString(),
                        imageWidth, imageHeight, lazyLoad: true);

                    if (i == 0)
                    {
                        ltrTopItem.Text = string.Format(TopItemFormat,
                                                        r[SqlConstants.Video.Url],
                                                        r[SqlConstants.Video.Name],
                                                        r[SqlConstants.Video.EncodedName],
                                                        avatar);
                    }
                    else
                    {
                        sb.AppendFormat(LiFormat,
                            r[SqlConstants.Video.Url],
                            r[SqlConstants.Video.Name],
                            r[SqlConstants.Video.EncodedName],
                            avatar);
                    }
                }

                if (sb.Capacity > 0)
                {
                    ltrOtherItems.Text = sb.ToString();
                }
                return true;
            }
            return false;
        }
    }
}